# encoding = utf8

from lib.tools import cxt_connetMyql
from conf.setting import MYSQL_INFO
# from lib.tools import cxt_myRequest
import requests

# 输入加密的用户名,手机号,身份证号和产品id,产出有信用额度的用户(开户绑卡需要去H5操作)
def authorize(userName,userMobile,userIdcard,productId,riskScore):
    # 调用加密解密接口对用户三要素进行加密
    lista = [userName, userMobile, userIdcard]
    listb = []
    url = 'http://apiv2.test3.sanwenqian.cn/api/user/enOrdecrypt'
    headerspass = {
        'pass': '32914'
    }

    for i in lista:
        data = {
            'text': i,
            'type': 'en'
        }
        resEn = requests.post(url, data, headers=headerspass).json()
        # 打印加密的信息
        # print(resEn['data'])
        listb.append(resEn['data'])

    # print(listb[0], listb[1], listb[2])
    userName = listb[0]
    userMobile = listb[1]
    userIdcard =listb[2]
    #
    insert_userIndex = "insert into tt_user_index (user_mobile, user_real_name, user_idcard_auth_status, user_info_auth_status, user_mobile_auth_status, user_zhima_auth_status, user_taobao_auth_status, user_jingdong_auth_status, user_credit_card_auth_status, user_shebao_auth_status, user_gongjijin_auth_status, user_salary_card_auth_status, user_medical_diagnosis_auth_status, user_worker_group_photo_auth_status, user_credit_money, user_available_money, user_credit_money_expire, user_credit_status, finace_product_id) \
    values ('" + userMobile + "', '" + userName + "', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8000, 8000, 1609124073, 8, '%d');" % productId

    # select_userMobile = "select * from tt_user_index where user_mobile= '{}';".format(whiteUserMobile)
    select_userMobile = "select * from tt_user_index where user_mobile= '%s';" % (userMobile)

    try:
        conn = cxt_connetMyql(**MYSQL_INFO)

    except Exception as e:
        print('链接数据库失败', e)

    finally:
        insertMobile = conn.executeSelectSql(select_userMobile)
        if insertMobile:
            print('该手机号已注册')

        conn.executeOtherSql(insert_userIndex)

        res = conn.executeSelectSql(select_userMobile)
        print('用户id为:', res[0]['user_id'])
        userId = res[0]['user_id']

        insert_idcardInfo = "insert into tt_user_idcard_info (user_id,user_real_name, user_id_card_no, user_face_photo_first, user_face_photo_second, user_face_photo_third, user_id_card_face, user_id_card_back, user_sex, user_nation, user_birthday, user_native_place, user_native_place_md5, user_native_province, user_native_city, user_native_area, user_idcard_start, user_idcard_end, user_issuing_authority) \
        values (%d,'%s', '%s','/20190409/102575/face-4fb2db33-144d-433e-86d2-481e6c9f138c.jpeg','/20190409/102575/face-89aa6624-472d-48e6-912d-b9f197007d14.jpeg','/20190409/102575/face-26c64b8d-5cab-42d5-9994-d31d21efc632.jpeg','/20190409/102575/cardFace-0a327f26-47b5-4d6f-84aa-e74e12d6ccff.jpeg','/20190409/102575/cardFace-0a327f26-47b5-4d6f-84aa-e74e12d6ccff.jpeg',2,'汉',677775600,'北京市海淀区增光路45号院中国劳动关系学院(2010专)','42af811dafaedba3d1fb64d314310f1b',13,02,29,1341849600,1657382400,'北京市公安局海淀分局');" \
                            % (userId, userName, userIdcard)

        insert_personalInfo = "insert into tt_user_personal_info(`user_living_address_lng`, `user_company_nature`, `user_company_address_lng`, `update_time`,\
                              `user_living_address_detail_md5`, `user_relation_mobile_third`, `user_relation_name_third`,\
                              `user_location_lat`, `user_relation_mobile_second`, `user_company_address_detail_md5`,\
                              `user_company_name`, `user_location_lng`, `user_relation_name_second`, `user_relation_third`,\
                              `user_work_duration`, `create_time`, `user_company_address_lat`, `wechart_account`, `email`,\
                              `user_relation_first`, `user_company_area`, `user_company_address_detail`,\
                              `user_work_company_mobile`, `user_relation_name_first`, `user_work_industry`,\
                              `user_company_province`, `user_living_area`, `occupation`, `user_company_address`, `user_id`,\
                              `user_loan_purpose`, `user_income`, `user_living_city`, `user_living_province`,\
                              `user_living_address_lat`, `user_relation_second`, `user_living_duration`,\
                              `user_living_address`, `user_marriage`, `user_work_post`, `user_edu`, `user_company_city`,\
                              `user_living_address_detail`, `user_relation_mobile_first`)\
        values('116.432969', '0', '116.432969', '1558073679', '4bea3c808d6bd6acd5d227c7f504a8e6',\
               'LPpy+hVdJFlSTnsy2bZfag==', '49v3ol4DIKuDbr61rOVu3Q==', '', 'uiMysgcYGNDGHVFwEzJSXQ==',\
               '82b58ecbb12117961e0cafa123221571', 'jfhdjdjfjfn', '', 'VyaOTeLQxWg+Aa4iYlRJ7g==', '7', '0', '1558073515',\
               '39.920465', 'jsdb', '简简单单就', '1', '濉溪县', '科技的贺卡', '858464', 'PagGCzZJcNGRCHxVv0764Q==', '0', '安徽', '东城区',\
               '', '安徽-淮北-濉溪县', '%d', '9', '3', '北京', '北京', '39.920465', '7', '0', '北京-北京-东城区', '2', '0', '2', '淮北',\
               'irufjdjjd', 'yL3PfcgR9KUr8uaTUcTMKg==');" % userId


        insert_yimeiInfo = "insert into tt_user_yimei_info (user_id, medical_project_name, medical_project_amount, medical_diagnosis_report, medical_diagnosis_status, worker_group_photo, worker_group_photo_status) values ('%d', '明明', 10000,'/20190409/102575/medicalDiagnosis-2917234e-8af7-4200-bd21-d2d04f9b497e.jpeg,/20190409/102575/medicalDiagnosis-e4ac53fd-b474-45e4-a95e-df4acc91d98f.jpeg',1, '/20190409/102575/workGroupPhoto-3b93ac54-3aca-486d-b3c3-b9170a5e3418.jpeg', 1);" % userId

        conn.executeOtherSql(insert_idcardInfo)

        conn.executeOtherSql(insert_personalInfo)
        if productId == 7:
            conn.executeOtherSql(insert_yimeiInfo)

        insert_risk_callmoney = "INSERT INTO tt_risk_cal_money(user_id, risk_score) VALUES(%d, %d);" % (userId, riskScore)
        select_riskId = "SELECT id FROM tt_risk_cal_money WHERE user_id=%d;" %userId
        conn.executeOtherSql(insert_risk_callmoney)
        riskRes = conn.executeSelectSql(select_riskId)
        riskId=riskRes[0]['id']
        update_userIndex_risk = "UPDATE tt_user_index SET risk_cal_money_id=%d;" %riskId
        conn.executeOtherSql(update_userIndex_risk)
        # update_order_risk = "UPDATE tt_order SET risk_cal_money_id=%d;" %riskId
        # conn.executeOtherSql(update_order_risk)


def deleteUser(userId):
    delete_personalInfo="DELETE FROM tt_user_personal_info WHERE user_id=%d;" %userId
    delete_idcardInfo="DELETE FROM tt_user_idcard_info WHERE user_id=%d;" %userId
    delete_userIndex="DELETE FROM tt_user_index WHERE user_id=%d;" %userId
    delete_riskCallmoney="DELETE FROM tt_user_index WHERE user_id=%d;" %userId
    delete_yimeiInfo="DELETE FROM tt_risk_cal_money WHERE user_id=%d;" %userId
    conn = cxt_connetMyql(**MYSQL_INFO)
    conn.executeOtherSql(delete_personalInfo)
    conn.executeOtherSql(delete_idcardInfo)
    conn.executeOtherSql(delete_userIndex)
    conn.executeOtherSql(delete_yimeiInfo)

# deleteUser(186316)

#test3 医美分期产品id: 7, 向心贷产品id: 11
#test1 向心贷产品id: 8

# 风控分值-->(风控等级,风控描述)
#
# 800~1000 --> (A , 低风险)
#
# 600~799 --> (B , 低风险)
#
# 500~599 --> (C , 中低风险)
#
# 400~499 --> (D , 中低风险)
#
# 0~399 --> (E , 高风险)

pin = 0  # 存的文件指针
with open('/Users/cuixuetao/Desktop/cxt_exercise/conf/testAccount.txt') as fr:
    fr.seek(pin)#移动指针
    for line in fr.readlines():
        print(line.split(','))
        tmp = line.split(',')
        # print(tmp[0],tmp[1],tmp[2])
        authorize(tmp[0],tmp[1],tmp[2],7,899)

    # pin = fr.tell() #记录已经读到了什么位置
    # print(pin)









